﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using compshop.Modules.OrdersToSupplier.Presenter;
using compshop.Infrastructure.CompShopDataService;

namespace compshop.Modules.OrdersToSupplier.Views
{
    /// <summary>
    /// Interaction logic for OrdersToSupplierView.xaml
    /// </summary>
    public partial class OrdersToSupplierView : UserControl, IViewOrdersToSupplier
    {
        public OrdersToSupplierPresenter  Presenter
        {
	        get 
	        {
                return DataContext as OrdersToSupplierPresenter;
	        }
	        set 
	        {
                DataContext = value;
                lvOrderList.Items.Clear();
                foreach (OrderToSupplier ord in Presenter.OrderToSupplierList)
                    lvOrderList.Items.Add(ord);
	        }
        }


        public OrdersToSupplierView()
        {
            InitializeComponent();
        }

        public OrdersToSupplierView(OrdersToSupplierPresenter presenter)
            :this()
        {
            Presenter = presenter;
            Presenter.View = this;
        }


        private void lvOrderList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lvOrderList.SelectedItem != null)
            {
                OrderToSupplier order = lvOrderList.SelectedItem as OrderToSupplier;
                if (order == null)
                    return;
                Presenter.OrderToSupplierSelectedPublisher((int)order.OrderToSupplierID);
            }
        }


        private void btnAddOrder_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ShowCreateOrderToSupplierDialog();
        }


        private void btnEditOrder_Click(object sender, RoutedEventArgs e)
        {
            if (lvOrderList.SelectedItem != null)
            {
                OrderToSupplier order = lvOrderList.SelectedItem as OrderToSupplier;
                if (order == null)
                    return;
                Presenter.ShowEditOrderToSupplierDialog(order);
            }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //lvOrderList.ItemsSource = Presenter.OrderToSupplierList;
            
        }

    }
}
